/**
 *  \file PublicTrigger.cpp
 *  \brief  This file defines an i3 public trigger.
 */

#include "OCD_include.h"

#include "PublicTrigger.h"
#include "I3OverlayNetwork.h"
#include "I3Id.h"
#include "i3CommonHeaders.h"

#include <string>

using namespace std;
/**
  * Constructor
  */
PublicTrigger::PublicTrigger
            (string i3DNS, string keyStr, string stack, string nameList,
             I3OverlayNetwork* overlay)
            
            :I3Trigger(overlay) {

    
    char tmpURL[MAX_URL_LEN];
    toLowerCase(i3DNS.c_str(), tmpURL);
    this->i3DNS = tmpURL;
    this->i3Key = new I3Key(keyStr);
    this->i3Id = NULL;  
    this->stack = stack;


    //associate the comma separated list of names in nameList with the last id in the stack.
    //The name list contains the name of the middleboxes in the id stack (except the last one),
    //which are to be authenticated if required.
    StringTokenizer st = StringTokenizer (stack, ",");
    string lastId = "";
    while (st.hasMoreTokens()) {
        lastId = st.nextToken();
    }
    overlay->addMiddleBoxNamesById (lastId, nameList);
    
    //TODO XXX This may need to be changed
    
}


/**
  * Gives a string representation (pretty printed) of the PublicTrigger
  * @param tmpStr a character buffer in which the string is stored.
  * Sufficient space should be allocated for tmpStr.
  * @return pointer to tmpStr
  */
char* PublicTrigger::toString(char *tmpStr) {

    char tmpBuf[MAX_PRINT_BUF];

    sprintf(tmpStr,
            "PublicTrigger:\n"
            "\tI3 DNS\t\t:\t%s\n"
            "\tI3 Key:\t%s\n"
            "\tI3 Stack:\t%s\n",
            i3DNS.c_str(),
            i3Key->toString(tmpBuf),
            stack.c_str()    
        );
    return tmpStr;
}


